package com.byxm.wlgw.control;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.mail.Flags.Flag;

import org.aspectj.util.FileUtil;

import com.byxm.wlgw.bean.DdxxBean;
import com.byxm.wlgw.bean.GwcxxBean;
import com.byxm.wlgw.bean.ShxxBean;
import com.byxm.wlgw.bean.YhxxBean;
import com.byxm.wlgw.common.ComDdxxTools;
import com.byxm.wlgw.common.ComUtil;

import com.byxm.wlgw.query.DdspxxQuery;
import com.byxm.wlgw.service.DdspxxService;
import com.byxm.wlgw.service.DdxxService;
import com.byxm.wlgw.service.GwcxxService;
import com.byxm.wlgw.service.ShxxService;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("unused")
public class DdxxAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	Map<String, Object> session; 
	private List<ShxxBean> lstShxx=null;//收货信息
	private List<GwcxxBean> lstGwcxx=null;//购物车信息
	
	private float spzjg=0;//总金额
	
	/**给前台的*/
	private DdxxBean ddxxForCli;
	
	/**前台传回的数据*/
	private String shdzid;//前台传回的收获地址ID
    private String fhfs;//前台传回的发货方式
    
   // Yhxx yhxxBean=null;//用户信息
    
   // private DdxxBean ddxxBean;
    private String ceshiyixia="";
    
    private String ddxxid="";
    
    private DdxxBean ddxxBean;
	ShxxService shxxService=new ShxxService();
	GwcxxService gwcxxService=new GwcxxService();
	
/**
 * 填写收货信息页面
 * @return
 */
	public String ready(){
		session=ActionContext.getContext().getSession();
		YhxxBean yhxxBean=(YhxxBean)session.get("yhxxSession");
		lstShxx=shxxService.getShxxByYhxxid(yhxxBean.getYhxxid());
		try {
			lstGwcxx=gwcxxService.getGwcxx(yhxxBean.getYhxxid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		spzjg=this.getGwcxxJsje(lstGwcxx);
		return "success_01";
	}
	
	/**
	 * 创建订单
	 * @return
	 */
	public String creat(){
		/***************Service**************/
		DdxxService ddxxService=new DdxxService();
		DdspxxService ddspxxService=new DdspxxService();
		GwcxxService gwcxxService=new GwcxxService();
		DdxxBean ddxx=null;
		
		session=ActionContext.getContext().getSession();
		String resultStr="";
		YhxxBean yhxxBean=null;
		List<GwcxxBean> lstGwcxx=null;
		if(session.get("yhxxSession")!=null){
			yhxxBean=(YhxxBean)session.get("yhxxSession");
			lstGwcxx=this.getGwcxxsByYhxxid(yhxxBean.getYhxxid());
		}else{
			resultStr="index";
		}
		if(lstGwcxx==null){
			resultStr="index";
		}else{
			GwcxxBean gwcxxBean=new GwcxxBean();
			gwcxxBean.setYhxxid(yhxxBean.getYhxxid());
			ddxx=new DdxxBean();
			ddxx=this.getDdxxQT();
			ddxx.setJyze(this.getGwcxxJsje(lstGwcxx));
			//插入订单信息
			ddxxService.insertDdxx(ddxx);
			try {
				if(ddspxxService.insertDdspxx(ddxx.getDdxxid(), lstGwcxx)){
					//清空购物车
					gwcxxService.deleteGwcxx(gwcxxBean);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			ddxxid=ddxx.getDdxxid();
			resultStr="success_02";
		}
		return resultStr;
//		if(session.get("gwcxxSession")!=null){
//			DdxxService ddxxService=new DdxxService();
//			DdspxxService ddspxxService=new DdspxxService();
//			GwcxxService gwcxxService=new GwcxxService();
//
//		List<GwcxxBean> lstGwcxx2=new ArrayList<GwcxxBean>();	
//		DdxxBean ddxx=new DdxxBean();
//
//		//订单信息,无总金额
//		ddxx=this.getDdxxQT();
//		//用户信息ID
//		int yhxxidls=ddxx.getYhxxid();
//		//获取购物车信息
//		lstGwcxx2=this.getGwcxxsByYhxxid(yhxxidls);
//		//设置总金额
//		ddxx.setJyze(this.getGwcxxJsje(lstGwcxx2));
//		//新建一个订单
//		ddxxService.insertDdxx(ddxx);
//		//输入订单商品信息
//		if(ddspxxService.insertDdspxx(ddxx.getDdxxid(), lstGwcxx2)){
//			GwcxxBean gwcxxDel=new GwcxxBean();
//			gwcxxDel.setYhxxid(lstGwcxx2.get(0).getYhxxid());
//			try {
//				//清空购物车
//				gwcxxService.deleteGwcxx(gwcxxDel);
//				//将购物车session也清空
//				session.remove("gwcxxSession");
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		ddxxForCli=ddxx;
//		return "success_02";
//		}else {
//			return "index";
//		}
	}
	
	public String find(){
		DdxxService ddxxService=new DdxxService();
		session=ActionContext.getContext().getSession();
		YhxxBean yhxxBean=null;
		if(session.get("yhxxSession")==null||ComUtil.isNull(ddxxid)){
			return "index";
		}else{
			yhxxBean=(YhxxBean)session.get("yhxxSession");
		}
		if(yhxxBean!=null&&!ComUtil.isNull(ddxxid)){
			ddxxBean=ddxxService.findDdxxBean(ddxxid);
			return "success_03";
		}else{
			return "index";
		}
	}
	
	/**
	 * 模拟支付
	 */
	public String update(){
		DdxxService ddxxService=new DdxxService();
		try {
			ddxxService.updateDdxxJyzt(ddxxBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "index";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**************私有方法********************/
	/**
	 * 获得当前用户购物车信息
	 */
	private List<GwcxxBean> getGwcxxsByYhxxid(int yhxxid){
		List<GwcxxBean> list=null;
		GwcxxService gwcxxService=new GwcxxService();
		try {
			list=gwcxxService.getGwcxxNoUrl(yhxxid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 生成订单信息
	 */
	private DdxxBean getDdxxQT(){
		session=ActionContext.getContext().getSession();
		YhxxBean bean=new YhxxBean();
		bean=(YhxxBean)session.get("yhxxSession");
		DdxxBean ddxx=new DdxxBean();
		ShxxService shxxService=new ShxxService();
		lstShxx=shxxService.getShxxByYhxxid(bean.getYhxxid());
		System.out.println("收获人姓名"+lstShxx.get(0).getShrxm());
	    String nowDate=null;//当天时间
	    int newYhxxid=0;//用户信息Id
        int fkzt=1;//付款状态;1：未付款 2：已付款 3：货到付款
        int fhzt=1;//发货状态  1：未发货 2：已发货 3：缺货需联系客户 协调事宜
        int jyzt=1;//交易状态 1：未交易成功2：交易成功2：交易关闭（24小时没付款，或者退货）考虑是否添加触发器
        
		if(shdzid.trim()!=""){//收获地址ID
			ddxx.setShxxid(Integer.parseInt(shdzid));
		}
		if(fhfs.trim()!=""){//发货方式
			ddxx.setFhfs(Integer.parseInt(fhfs));
		}
		//订单信息ID
		ddxx.setDdxxid(this.getDdxxbh());
		//订单建立日期
		ddxx.setDdjlsj(this.getNowTime());
		//用户信息ID
	    ddxx.setYhxxid(bean.getYhxxid());
	    //付款状态
	    ddxx.setFkzt(fkzt);
	    //发货状态
	    ddxx.setFhzt(fhzt);
	    //交易状态
	    ddxx.setJyzt(jyzt);
	    //交易总额
	    ddxx.setJyze(this.getGwcxxJsje(this.getGwcxxsByYhxxid(bean.getYhxxid())));
	    //收获地址
	    ddxx.setShdz(lstShxx.get(0).getShdz());
	    //手机号码
	    ddxx.setSjhm(lstShxx.get(0).getSjhm());
	    //邮编
	    ddxx.setYb(lstShxx.get(0).getYb());
	    //收获人姓名
	    ddxx.setShrxm(lstShxx.get(0).getShrxm());
	    
	    System.out.println("发货状态："+ddxx.getFhzt());
	    System.out.println("收获人姓名："+ddxx.getShrxm());
	    
	    return ddxx;
	}
	
	/**
	 * 生成订单信息编号
	 * @return
	 */
	private String getDdxxbh(){
		String ddxxBhStr="";
		ComDdxxTools comDdxxTools=new ComDdxxTools();
		ddxxBhStr=comDdxxTools.getDdxxid();
		return ddxxBhStr;
	}

	/**
	 * 结算总金额  
	 * @param 购物车信息List
	 * @return
	 */
	private float getGwcxxJsje(List<GwcxxBean> list){
		float fjsje=0;
		for(int i=0;i<list.size();i++){
			fjsje+=(list.get(i).getSpsj())*(list.get(i).getSpsl());
		}
		return fjsje;
		}
	
   /**
    * 获取当前日期和时间   
    * @param dateformat
    * @return
    */
    private String getNowTime(){   
        Date date = new Date();   
        String dateStr="";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式    
        dateStr = dateFormat.format(date);      
        return dateStr;   
    }
    
	/**************setter getter********************/
	
	public List<ShxxBean> getLstShxx() {
		return lstShxx;
	}

	public void setLstShxx(List<ShxxBean> lstShxx) {
		this.lstShxx = lstShxx;
	}
	public List<GwcxxBean> getLstGwcxx() {
		return lstGwcxx;
	}

	public void setLstGwcxx(List<GwcxxBean> lstGwcxx) {
		this.lstGwcxx = lstGwcxx;
	}
	public float getSpzjg() {
		return spzjg;
	}

	public void setSpzjg(float spzjg) {
		this.spzjg = spzjg;
	}
	public String getShdzid() {
		return shdzid;
	}

	public void setShdzid(String shdzid) {
		this.shdzid = shdzid;
	}
	
	public String getFhfs() {
		return fhfs;
	}

	public void setFhfs(String fhfs) {
		this.fhfs = fhfs;
	}
	public DdxxBean getDdxxForCli() {
		return ddxxForCli;
	}

	public void setDdxxForCli(DdxxBean ddxxForCli) {
		this.ddxxForCli = ddxxForCli;
	}

	public String getCeshiyixia() {
		return ceshiyixia;
	}

	public void setCeshiyixia(String ceshiyixia) {
		this.ceshiyixia = ceshiyixia;
	}
	
	public DdxxBean getDdxxBean() {
		return ddxxBean;
	}

	public void setDdxxBean(DdxxBean ddxxBean) {
		this.ddxxBean = ddxxBean;
	}

	public String getDdxxid() {
		return ddxxid;
	}

	public void setDdxxid(String ddxxid) {
		this.ddxxid = ddxxid;
	}



}
